package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.TeamManagement;
import com.ruoyi.base.service.ITeamManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组管理Controller
 * 
 * @author linchong
 * @date 2022-08-16
 */
@Api(value = "班组管理",tags = "班组管理")
@RestController
@RequestMapping("/base/management")
public class TeamManagementController extends BaseController
{
    @Autowired
    private ITeamManagementService teamManagementService;

    /**
     * 查询班组管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeamManagement teamManagement)
    {
        startPage();
        List<TeamManagement> list = teamManagementService.selectTeamManagementList(teamManagement);
        return getDataTable(list);
    }

    /**
     * 导出班组管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:management:export')")
    @Log(title = "班组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeamManagement teamManagement) {
        List<TeamManagement> list = teamManagementService.selectTeamManagementList(teamManagement);
        ExcelUtil<TeamManagement> util = new ExcelUtil<TeamManagement>(TeamManagement.class);
        util.exportExcel(response, list, "班组管理数据");
    }

    /**
     * 获取班组管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(teamManagementService.selectTeamManagementById(id));
    }

    /**
     * 新增班组管理
     */
    @PreAuthorize("@ss.hasPermi('base:management:add')")
    @Log(title = "班组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeamManagement teamManagement) {
        if (StringUtils.isBlank(teamManagement.getTeamName())) {
            return AjaxResult.error("班组名称不能为空!");
        }
        return toAjax(teamManagementService.insertTeamManagement(teamManagement));
    }

    /**
     * 修改班组管理
     */
    @PreAuthorize("@ss.hasPermi('base:management:edit')")
    @Log(title = "班组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeamManagement teamManagement) {
        TeamManagement team = teamManagementService.selectTeamManagementById(teamManagement.getId());

        return toAjax(teamManagementService.updateTeamManagement(teamManagement));
    }

    /**
     * 删除班组管理
     */
    @PreAuthorize("@ss.hasPermi('base:management:remove')")
    @Log(title = "班组管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        return toAjax(teamManagementService.deleteTeamManagementByIds(ids));
    }


    @ApiOperation("班组状态启用")
    @PostMapping("/statusEnable")
    @Log(title = "班组管理", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('base:management:enable')")
    public AjaxResult teamStatusEnable(@RequestBody Long[] ids){
        if (ids.length == 0) {
            return AjaxResult.error("id不能为空！");
        }
        int i = teamManagementService.teamStatusEnable(ids);
        return AjaxResult.success();
    }

    @ApiOperation("班组状态禁用")
    @Log(title = "班组管理", businessType = BusinessType.OTHER)
    @PostMapping("/statusDisable")
    @PreAuthorize("@ss.hasPermi('base:management:disable')")
    public AjaxResult teamStatusDisable(@RequestBody Long[] ids){
        if (ids.length == 0) {
            return AjaxResult.error("id不能为空！");
        }
        int i = teamManagementService.teamStatusDisable(ids);
        return AjaxResult.success();
    }

    /**
     * 班组状态切换状态
     * @param teamManagement
     * @return
     */
    @Log(title = "班组管理", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('base:management:disable')")
    @PutMapping("/status")
    public AjaxResult teamStatus(@RequestBody TeamManagement teamManagement){
        if (teamManagement.getId() == null){
            return AjaxResult.error("班组id不能为空！");
        }
        if (StringUtils.isBlank(teamManagement.getTeamStatus())){
            return AjaxResult.error("班组状态不能为空！");
        }
        teamManagementService.teamStatusChange(teamManagement.getId(),teamManagement.getTeamStatus());
        return AjaxResult.success();
    }


}
